package com.egene.zh.activity.login;

import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.egene.zh.R;
import com.egene.zh.activity.main.MainActivity_;
import com.egene.zh.api.LoginApi;
import com.egene.zh.base.BaseActivity;
import com.egene.zh.dao.UserDao;
import com.egene.zh.model.User;
import com.egene.zh.model.net.ResponseBean;
import com.egene.zh.utils.JsonUtil;
import com.egene.zh.utils.MToast;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

@EActivity(R.layout.activity_login)
public class LoginActivity extends BaseActivity {
    @ViewById
    EditText etAccount;
    @ViewById
    EditText etPw;

    @Bean
    UserDao userDao;

    @AfterViews
    void deal(){
        User user = userDao.getUser();
        if(!TextUtils.isEmpty(user.getLoginID())){
            etAccount.setText(user.getLoginID());
            etPw.setText(user.getPassword());
            doLogin(user.getLoginID(), user.getPassword());
        }
    }



    /**
     * 登录
     */
    @Click(R.id.btnLogin)
    void login() {
        if (TextUtils.isEmpty(etAccount.getText())) {
            MToast.show(R.string.please_enter_account);
            return;
        }
        if (TextUtils.isEmpty(etPw.getText())) {
            MToast.show(R.string.please_enter_password);
            return;
        }

        doLogin(etAccount.getText().toString(), etPw.getText().toString());

    }

    void doLogin(String account, String pw){
        showWaitDialog(getString(R.string.signing)).show();
        LoginApi.login(account, pw, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                ResponseBean<User> result = JsonUtil.toBean(responseBody,
                        new TypeToken<ResponseBean<User>>() {
                        }.getType());
                if (result != null) {
                    if (result.isSuccess()) {
                        userDao.setUser(result.getData());
                        String pw = etPw.getText().toString();
                        if(!TextUtils.isEmpty(pw)){
                            userDao.setPw(pw);
                        }
                        loginToHX();
                        goTo(MainActivity_.class);
                        finish();
                    }else {
                        MToast.show(result.getMessage());
                        userDao.removeUser();
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }

            @Override
            public void onFinish() {
                hideWaitDialog();
                super.onFinish();
            }
        });
    }

    void loginToHX(){
        EMChatManager.getInstance().login(userDao.getUser().getLoginID(),"111111",new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        EMGroupManager.getInstance().loadAllGroups();
                        EMChatManager.getInstance().loadAllConversations();
                        Log.d("main", "登陆聊天服务器成功！");
                    }
                });
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登陆聊天服务器失败！");
            }
        });
    }

    /**
     * 注册
     */
    @Click(R.id.btnRegister)
    void register() {
        goTo(RegisterActivity_.class);
    }

}
